os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/System/API/zip")
local run = true
local xSize, ySize = term.getSize()
local obj = {}
local screen = 1
local maxScreen = 5
local instFolder = "/.ds/"..l_login..'/'
local objName = {}
local id = 0
local mods = ""

local newObj = function(name,x1,y1,x2,y2)
 obj[name] = {}
 obj[name]["x1"] = x1
 obj[name]["y1"] = y1
 obj[name]["x2"] = x2
 obj[name]["y2"] = y2
end

local pastebin = function(paste,filename)
 local file = http.get("http://pastebin.com/raw.php?i="..paste)
 if file then
  file = file.readAll()
  h=fs.open(filename,"w")
  h.write(file)
  h.close()
 else
  windows.error("Failed to connect to pastebin.com")
 end
end

local clear = function(color)
 term.setBackgroundColor(color)
 term.clear()
 term.setCursorPos(1,1)
end

local usualText = function(string,xPos,yPos)
 term.setCursorPos(xPos,yPos)
 term.write(string)
end
 

local sysBar = function()
 local time = textutils.formatTime(os.time(),true)
 term.setTextColor(colors.lightGray)
 term.setBackgroundColor(colors.white)
 usualText("Store@",1,1)
 sysbar.draw(colors.white,colors.black,noty,true,false)
 paintutils.drawLine(xSize,1,xSize,ySize,colors.gray)
 paintutils.drawLine(xSize,(ySize/maxScreen*screen)-(ySize/maxScreen)+1,xSize,ySize/maxScreen*screen,colors.white)
 term.setTextColor(colors.gray)
 usualText(screen,xSize,(ySize/maxScreen*screen)-(ySize/maxScreen)+1)
end

local app = function(name,x,y,info,by)
 image.draw(x,y,"/store-icons/"..name..".png")
 term.setBackgroundColor(colors.white)
 term.setTextColor(colors.lime)
 usualText(name,x+7,y)
 term.setBackgroundColor(colors.white)
 term.setTextColor(colors.black)
 usualText(info,x+7,y+1)
 term.setTextColor(colors.lightGray)
 term.setBackgroundColor(colors.white)
 usualText("By ",x+7,y+2)
 term.write(by)
 mods = " Install "
 term.setTextColor(colors.white)
 term.setBackgroundColor(colors.lime)
 local files = fs.list(instFolder)
 for i=1,#files do 
  if files[i] == name..".apk" then
   mods=" Update "
   term.setTextColor(colors.lime)
   term.setBackgroundColor(colors.white)
  end
 end
 if name == 'Store@' then 
  term.setTextColor(colors.lime)
  term.setBackgroundColor(colors.white)
  mods = ' Update '
 end
 usualText(mods,xSize-1-#mods,y+3)
 newObj(name,xSize-1-#mods,y+3,xSize-2,y+3)
 newObj(name..".inf",2,y,xSize-2,y+2)
 id=id+1
 objName[id] = name
end

local delObj = function()
 for i=1,(screen-1)*3 do
  obj[objName[i]]["x1"]=-1
  obj[objName[i]]["y1"]=-1
  obj[objName[i]]["x2"]=-1
  obj[objName[i]]["y2"]=-1
 end
end

local appinfo = function(name,by)
 clear(colors.white)
 sysBar()
 image.draw(2,3,"/store-icons/"..name..".png")
 term.setBackgroundColor(colors.white)
 term.setTextColor(colors.lime)
 usualText(name,9,3)
 term.setTextColor(colors.lightGray)
 usualText("By: ",9,4)
 term.write(by)
 mods = " Install "
 term.setTextColor(colors.white)
 term.setBackgroundColor(colors.lime)
 local files = fs.list(instFolder)
 for i=1,#files do 
  if files[i] == name..".apk" then
   mods=" Update "
   term.setTextColor(colors.lime)
   term.setBackgroundColor(colors.white)
  end
 end
 usualText(mods,xSize-1-#mods,6)
 newObj(name..".inf",xSize-1-#mods,5,xSize-2,5)
 id=id+1
 objName[id] = name..".inf"
end

local redraw = function()
 clear(colors.white)
 sysBar()
 if screen == 1 then
  id = 0
  delObj()
  app("Store@",2,3,"Apps Store","-DECE-")
  app("ImageViewer",2,9,"Image browser","-DECE-")
  app("FileBrowser",2,15,"File manager","-DECE-")
 elseif screen == 2 then
  delObj()
  app("Photoshop",2,3,"Image editor","ECS")
  app("Ink",2,9,"Text editor","OEED"," Install ")
  app("QuickShell",2,15,"Shell","OEED"," Install ")
 elseif screen == 3 then
  delObj()
  app("Maze3D",2,3,"3D game","JESUSTHEKILLER")
  app("NanoMaze",2,9,"Mazes in CC","JESUSTHEKILLER")
  app("Minesweeper",2,15,"Game","GopherAtl")
 elseif screen == 4 then
  delObj()
  app("FlappyPixel",2,3,"Game","Creator")
  app("gtRemote",2,9,"Remote turtle","Dog")
  app("Udo",2,15,"Pocket Todo List","Zambonie")
 elseif screen == 5 then
  delObj()
  app("PackageInstaller",2,3,"For custom apps","-DECE-")
  app("Backup",2,9,"Backup/restore","-DECE-")
 end
end

local inst = function(name,code,path)
 shell.run("rm "..path..name..".apk")
 clear(colors.white)
 term.setTextColor(colors.black)
 usualText("Installing "..name,2,ySize-6)
 usualText("Please wait...",2,ySize-5)
 paintutils.drawLine(2,ySize-2,xSize-1,ySize-2,colors.lightGray)
 paintutils.drawLine(2,ySize-2,2,ySize-2,colors.lime)
 pastebin(code,path..name..".apk/main")
 paintutils.drawLine(3,ySize-2,3+5,ySize-2,colors.lime)
 shell.run("mkdir "..path..name..".apk/Resources")
 paintutils.drawLine(9,ySize-2,8+5,ySize-2,colors.lime)
 sleep(0.5)
 shell.run("copy /store-icons/"..name..".png "..path..name..".apk/Resources")
 paintutils.drawLine(14,ySize-2,14+5,ySize-2,colors.lime)
 sleep(0.5)
 shell.run("rename "..path..name..".apk/Resources/"..name..".png "..path..name..".apk/Resources/icon.png")
 paintutils.drawLine(20,ySize-2,25,ySize-2,colors.lime)
 sleep(0.5)
 clear(colors.white)
 if noty ~= nil then
  noty[#noty+1]={["app"]=name, ["text"]=name.." installed", ["button"]=" Launch ", ["ico"]=string.char(25)}
 else
  noty[1]={["app"]=name, ["text"]=name.." installed", ["button"]=" Launch ", ["ico"]=string.char(25)}
 end
 redraw()
end

local running = function()
 while run == true and l_login ~= '...' and l_login ~= nil and l_login ~= '' do
  event, side, x, y = os.pullEvent()
 
  if event == "mouse_scroll" then
   if side == 1 then
    screen = screen+1
    if screen > maxScreen then
     screen = maxScreen
    else
     clear(colors.white)
     redraw()
    end
   elseif side == -1 then
    screen = screen-1
    if screen < 1 then
     screen = 1
    else
     clear(colors.white)
     redraw()
    end
   end
   
  elseif event == "key" then
   if side == 208 then
    screen = screen+1
    if screen > maxScreen then
     screen = maxScreen
    else
     clear(colors.white)
     redraw()
    end
   elseif side == 200 then
    screen = screen-1
    if screen < 1 then
     screen = 1
    else
     clear(colors.white)
     redraw()
    end
   end

  elseif event == "mouse_click" then
   if sysbar.back(x,y) == true then
    term.clear()
    term.setCursorPos(1,1)
    run = false 
   elseif sysbar.home(x,y) == true then
    term.clear()
    term.setCursorPos(1,1)
	home = true
    run = false
   end
   if screen == 1 then
    if x>=obj["Store@"]["x1"] and x<=obj["Store@"]["x2"] and y>=obj["Store@"]["y1"] and y<=obj["Store@"]["y2"] and screen==1 then
     inst("Store@","g8LMzmxa",'/.ds/sys/')
	 run = false
	 break
    elseif x>=obj["ImageViewer"]["x1"] and x<=obj["ImageViewer"]["x2"] and y>=obj["ImageViewer"]["y1"] and y<=obj["ImageViewer"]["y2"] then
     inst("ImageViewer","tYNEKygi",instFolder)
    elseif x>=obj["FileBrowser"]["x1"] and x<=obj["FileBrowser"]["x2"] and y>=obj["FileBrowser"]["y1"] and y<=obj["FileBrowser"]["y2"] then
     inst("FileBrowser","2Xms362b",instFolder)
     pastebin("daqgga1q","/icons.zip")
     zip.unarchive('/icons.zip','/')
     shell.run('rm /icons.zip')
     pastebin("pDdRdBkj","/.ds/"..l_login.."/FileBrowser.apk/Resources/reader")
    end
   elseif screen == 2 then
    if x>=obj["QuickShell"]["x1"] and x<=obj["QuickShell"]["x2"] and y>=obj["QuickShell"]["y1"] and y<=obj["QuickShell"]["y2"] then
     inst("QuickShell","wwVmBJax",instFolder)
    elseif x>=obj["Ink"]["x1"] and x<=obj["Ink"]["x2"] and y>=obj["Ink"]["y1"] and y<=obj["Ink"]["y2"] then
     inst("Ink","JxzXPv03",instFolder)
    elseif x>=obj["Photoshop"]["x1"] and x<=obj["Photoshop"]["x2"] and y>=obj["Photoshop"]["y1"] and y<=obj["Photoshop"]["y2"] then
     inst("Photoshop","tK3FSmf2",instFolder)
    elseif x>=obj["Photoshop.inf"]["x1"] and x<=obj["Photoshop.inf"]["x2"] and y>=obj["Photoshop.inf"]["y1"] and y<=obj["Photoshop.inf"]["y2"] then
     appinfo("Photoshop", "ECS")
    end
   elseif screen == 3 then
    if x>=obj["Maze3D"]["x1"] and x<=obj["Maze3D"]["x2"] and y>=obj["Maze3D"]["y1"] and y<=obj["Maze3D"]["y2"] then
     inst("Maze3D","i7VkRaf6",instFolder)
    elseif x>=obj["NanoMaze"]["x1"] and x<=obj["NanoMaze"]["x2"] and y>=obj["NanoMaze"]["y1"] and y<=obj["NanoMaze"]["y2"] then
     inst("NanoMaze","m6WFB8D3",instFolder)
    elseif x>=obj["Minesweeper"]["x1"] and x<=obj["Minesweeper"]["x2"] and y>=obj["Minesweeper"]["y1"] and y<=obj["Minesweeper"]["y2"] then
     inst("Minesweeper","nsKrHTbN",instFolder)
    end
   elseif screen == 4 then
    if x>=obj["FlappyPixel"]["x1"] and x<=obj["FlappyPixel"]["x2"] and y>=obj["FlappyPixel"]["y1"] and y<=obj["FlappyPixel"]["y2"] then
     inst("FlappyPixel","4kqJKA42",instFolder)
    elseif x>=obj["gtRemote"]["x1"] and x<=obj["gtRemote"]["x2"] and y>=obj["gtRemote"]["y1"] and y<=obj["gtRemote"]["y2"] then
     inst("gtRemote","Cz31MiDJ",instFolder)
     pastebin("XQQsRyLX","/.ds/"..l_login.."/gtRemote.apk/Resources/app")
    elseif x>=obj["Udo"]["x1"] and x<=obj["Udo"]["x2"] and y>=obj["Udo"]["y1"] and y<=obj["Udo"]["y2"] then
     inst("Udo","LKguNrXk",instFolder)
    end
   elseif screen == 5 then
    if x>=obj["PackageInstaller"]["x1"] and x<=obj["PackageInstaller"]["x2"] and y>=obj["PackageInstaller"]["y1"] and y<=obj["PackageInstaller"]["y2"] then
     inst("PackageInstaller","vVeK5iuD",instFolder)
    elseif x>=obj["Backup"]["x1"] and x<=obj["Backup"]["x2"] and y>=obj["Backup"]["y1"] and y<=obj["Backup"]["y2"] then
     inst("Backup","U6nc5vK1",instFolder)
    end
--   else
--    redraw()
   end
  end
 end
end

local start = function()
 redraw()
 running()
end

local fadeX = function()
 clear(colors.gray)
 sleep(0.05)
 clear(colors.lightGray)
 sleep(0.05)
 clear(colors.white)
end

fadeX()
term.setTextColor(colors.black)
usualText("Loading...",xSize-10,19)
pastebin("YAjRjaJM","/store-icons.zip")
zip.unarchive("/store-icons.zip","/")
if l_login == '...' or l_login == nil or l_login == '' then
	pastebin("AH3k8DMa","ver")
	shell.run('ver')
end
start()
fadeX()
shell.run("rm /store-icons")
shell.run("rm /store-icons.zip")
clear(colors.black)